package com.cloudcc.mobile.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.NearByScreenAdapter;
import com.cloudcc.mobile.entity.NearByFilterEntity;
import com.cloudcc.mobile.entity.TwoOb;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.nostra13.universalimageloader.utils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lin on 2017/7/7.
 */

public class NearByMapScreenActivity extends BaseActivity {

    @Bind(R.id.near_by_screen_left_list)
    ListView nearByScreenLeftList;
    @Bind(R.id.near_by_screen_right_list)
    ListView nearByScreenRightList;
    @Bind(R.id.near_by_screen_back)
    TextView nearByScreenBack;

    private NearByScreenAdapter adapter, adapterRight;
    private List<NearByFilterEntity> lists;
    private List<TwoOb> twoObList;//当前选中第一列的第二列数据

    private String oneName, twoName;//第一个选中名 ， 第二个选中名

    public static int nearbySelect1 , nearbySelect2;
    public boolean isfrist;

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);

        Bundle bundle = getIntent().getBundleExtra("bundata");
        lists = (List<NearByFilterEntity>) bundle.getSerializable("data");

        twoObList = new ArrayList<>();

        adapter = new NearByScreenAdapter(this, 1);
        adapterRight = new NearByScreenAdapter(this, 2);
        nearByScreenLeftList.setAdapter(adapter);
        nearByScreenRightList.setAdapter(adapterRight);
        adapter.addData(lists);
        isfrist = true;//是刚刚进来
        adapter.setSelete(isfrist);
        adapterRight.setSelete(isfrist);
        if(nearbySelect1 >= 0){
            selectOne(nearbySelect1);
        }
        initLister();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_near_by_screen;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void initLister() {
        nearByScreenLeftList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //改变不是第一次进来的状态
                isfrist = false;
                adapter.setSelete(isfrist);
                adapterRight.setSelete(isfrist);
                selectOne(position);
            }
        });

        nearByScreenRightList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //改变不是第一次进来的状态
                isfrist = false;
                adapter.setSelete(isfrist);
                adapterRight.setSelete(isfrist);

                nearbySelect2 = position;
                twoName = twoObList.get(position).twoname;
                Intent intent = new Intent();
                intent.putExtra("onename",oneName);
                intent.putExtra("twoname",twoName);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        nearByScreenBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void selectOne(int position){
        oneName = lists.get(position).onename;
        twoObList = lists.get(position).twolist;
        adapterRight.clear();
        adapterRight.addData(twoObList);
        adapter.setSelete(position);
        adapter.notifyDataSetChanged();
        nearbySelect1 = position;
    }
}
